<?php

/**
 * Message actions.
 *
 * @package    wills
 * @subpackage Message
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class MessageActions extends sfActions
{
  public function preExecute()
  {
    $this->contestant = Doctrine::getTable('Contestant')->findOneBy(
      'id', 
      $this->getRequest()->getParameter('contestant_id')
    );

    $this->forward404Unless($this->contestant);
  }

  public function executeIndex(sfWebRequest $request)
  {
    $this->messages = $this->contestant
      ->getMessagesWithAuthorQuery()->execute();;
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MessageForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	//$this->form->bind(array('captcha'   => $request->getParameter('captcha'),));

    $form->bind($request->getParameter($form->getName()),$request->getFiles($form->getName()),$request->getParameter('captcha'));
    $message = $form->getObject()
        ->setContestantId($this->contestant->getId());
	if ($form->isValid())
    {
      $form->save();
		$this->getUser()->setFlash('message', '恭喜您，发表留言成功！');
      $this->redirect('@contestant_show?id=' . $this->contestant->getId());
    }

  }
}
